<?php
namespace app\admin\Controller;
use       think\Controller;
use       think\Db;
use       think\Request;
use       think\Cache;


/**
* 提现模块类
*/
class Getmoney extends Controller
{
	
	public function getmoney_list()
	{
		$list =Db::table('x_getmoney_list')->where('status',0)->select();
        $count=Db::table('x_getmoney_list')->where('status',0)->count();
        $this->assign('count',$count);
        $this->assign('list',$list);
        return $this->fetch();
	}
	//审核通过
    public function examine_pass()
    {
        $id =Request::instance()->post('id');
        $Data   = array('status'=>1);
        //更新状态
        $Offest =Db::table('x_getmoney_list')->where('id',$id)->update($Data);
        //得到openid
        $openid=Db::table('x_weixin_user')->where('id',$person['user_id'])->find();
        $this->send_template_message($openid['openid'],"您的提现审核已经通过,请注意查收！");
        //返回操作数据库状态
        if($Offest){return json(['code'=>1]);}else{return json(['code'=>0]);}
    }
    //审核拒绝
    public function examine_stop()
    {
        $id =Request::instance()->post('id');
        $remarks=Request::instance()->post('remarks');
        $Data   = array('status'=>-1,'remarks'=>$remarks);
        //得到user_id 减掉收益金额
        $person=Db::table('x_getmoney_list')->where('id',$id)->find();
        Db::table('x_user')->where('user_id',$person['user_id'])->setInc('profit', $person['money']);
        //更新状态
        $Offest =Db::table('x_getmoney_list')->where('id',$id)->update($Data);
        //返回操作数据库状态
        //得到openid
        $openid=Db::table('x_weixin_user')->where('id',$person['user_id'])->find();
        $this->send_template_message($openid['openid'],"您的提现申请被拒绝,请注意查收！");
        if($Offest){return json(['code'=>1]);}else{return json(['code'=>0]);}
    }
    //curl
    public function  http_curl($url,$data=null){
    //1.初始化，创建一个新cURL资源
        $ch = curl_init();
        //2.设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $url);
        //curl_setopt($ch, CURLOPT_HEADER, 0);
        if(!empty($data)){
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//不检测服务器的证书是否由正规浏览器认证过的授权CA颁发
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//不检测服务器的域名与证书上的是否一致
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//禁止curl资源直接输出
        //3.抓取URL并把它传递给浏览器
        $opt=curl_exec($ch);
        //4.关闭cURL资源，并且释放系统资源
        curl_close($ch);
        return $opt;
    }
    //获取access_token
    public function getAccess_token()
    {
        $appid="wxcd4dce13c9fb1a0a";   //微信公众号唯一标识
        $secret="a9f3c9753b70b42573fdd6440090c1c6";  //秘钥
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $json=$this->http_curl($url);
        $result=json_decode($json);
        Cache::set('access_token',$result->access_token,7000);
        return $result->access_token;
    }
    //发送模板消息
    public function send_template_message($openid,$message)
    {
        //缓存
        if(!Cache::get('access_token'))
        {
            $this->getAccess_token();
        }
        $access_token=Cache::get('access_token');
        $access_token= $this->getAccess_token();
        $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";
        $data=$this->package_data($openid,$message);
        $res=$this->http_curl($url,$data);
    }
    //模板消息参数封装
    public function package_data($openid,$message)
    {
        $data=array(         
            "touser"=>$openid,
            "template_id"=>"hk-zwotOfDQ08usR4jbLF5QxBgSrdFITP9_c0IIUthI",
            "url"=>"",
            "topcolor"=>"#FF0000",
            "data"=>array(
                 "first"=> array(  
                  "value"=> $message,  
                "color"=> "#173177"  
            ),  
            "keyword1"=> array(  
                "value"=> "####",  
                "color"=> "#173177"  
            ),  
            "keyword2"=> array(  
                "value"=>date("Y-m-d"),  
                "color"=> "#173177"  
            ),          
            "remark" =>array(
                 "value"=> "你的提现申请,我们已审核.",  
                 "color"=> "#173177"
             ),  
           )
        
            ); 

        return json_encode($data);
    }

}


?>